package com.itheima.reggie.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.PageBean;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;


@Api(tags = "员工管理")
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private HttpSession session;

    /**
     * 员工登录

     * @param employee
     * @return
     */
    @ApiOperation("员工登录")
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee){
        R<Employee> loginResult = employeeService.login(employee);
        //判定是否登录成功，如果是，将此用户存到session域中
        if (loginResult.getCode() ==1){
            session.setAttribute("employee",loginResult.getData().getId());
        }
        return loginResult;
    }

    /**
     * 员工登出
     * @param request
     * @return
     */
    @ApiOperation("员工登出")
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //销毁session
        session.removeAttribute("employee");
        return  R.success("退出成功");

    }

    /**
     * 分页条件查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @ApiOperation("员工分页查询")
    @GetMapping("/page")
    public R<PageBean> page(int page, int pageSize,String name){
        R<PageBean> employeePageBean = employeeService.findByPage(page, pageSize,name);
        return employeePageBean;
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @ApiOperation("添加员工")
    @PostMapping
    public R<String> addEmployee(@RequestBody Employee employee){

        //获取当前登录用户id
        Long loginEmployeeId = (Long) session.getAttribute("employee");
        //设置此添加操作的用户id
        employee.setCreateUser(loginEmployeeId);
        employee.setUpdateUser(loginEmployeeId);
        return employeeService.addEmployee(employee);

    }

    /**
     * 修改员工状态  status为1时正常，0或其他数字为封禁
     * 修改员工信息
     * @param employee
     * @return
     */
    @ApiOperation("更新员工信息")
    @PutMapping
    public R<String> updateEmployee(@RequestBody Employee employee){
        //获取当前登录用户id
        Long loginEmployeeId = (Long) session.getAttribute("employee");
        //设置此更新操作的用户id
        employee.setUpdateUser(loginEmployeeId);
        //设置更新时间
        employee.setUpdateTime(LocalDateTime.now());
        R<String> result = employeeService.updateEmployee(employee);
        return result;
    }


    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @ApiOperation("根据id查询员工")
    @GetMapping("/{id}")
    public R<Employee> findEmployeeById(@PathVariable("id")int id){
       R<Employee> result = employeeService.findEmployeeById(id);
       return result;
    }



}
